package com.client.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.action.AbstractAction;
import com.auth.service.AccountService;
import com.auth.service.AccountServiceImpl;
import com.client.model.ICpUserData;
import com.client.service.CpUserDataService;
import com.client.service.CpUserDataServiceImpl;
import com.manage.cache.ChannelManager;
import com.manage.cache.ProjectManager;
import com.manage.model.Channel;
import com.manage.model.Project;
import com.util.ProvinceUtil;
import com.util.RegionUtil;

public class CpUserDataQueryAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public CpUserDataQueryAction()
	{
		pageTag = -1;
	}

	private String date1;
	private String date2;
	private int plugin;
	private int channelId;
	private int projectId;
	private String province;
	private String region;
	private String dimension;
	private List<Channel> channels;
	private List<Project> projects;
	private List<ICpUserData> datas;
	
	private int pageTag;
	
	public String data()
	{
		if (pageTag < 0)
		{
			date1 = formatDate(new Date(), "yyyy-MM") + "-01";
			date2 = formatDate(new Date(), "yyyy-MM-dd");
			channels = new ArrayList<Channel>();
			AccountService as = new AccountServiceImpl();
			List<Integer> channelIds = as.getAccountChannel(operator.getId());
			if (channelIds != null)
			{
				for (Channel c : ChannelManager.getInstance().getChannels())
				{
					if (channelIds.contains(c.getId()))
						channels.add(c);
				}
			}
			projects = new ArrayList<Project>();
			for (Project p : ProjectManager.getInstance().getProjects())
			{
				if (channelIds.contains(p.getChannelId()))
					projects.add(p);
			}
		}
		else
		{
			channels = ChannelManager.getInstance().getChannels();
			projects = ProjectManager.getInstance().getProjects();
			CpUserDataService service = new CpUserDataServiceImpl();
			if(province!=null && !province.equals("") && !province.equals("ALL"))
			{
				if(region!=null && !region.equals("") && !region.equals("ALL"))
					region = RegionUtil.getInstance().getRegion(province, region).getName();
				province = ProvinceUtil.getInstance().getProvince(province).getName();
			}
			datas =	service.getCpUserData(operator, date1, date2,
					1, channelId, projectId, province, region, dimension);
		}
		return SUCCESS;
	}
	
	
	public String changeprovince()
	{
		return SUCCESS;
	}
	
	public String changechannel()
	{
		projects = ProjectManager.getInstance().getProjects(channelId);
		return SUCCESS;
	}
	
	public String getDate1() {
		return date1;
	}

	public void setDate1(String date1) {
		this.date1 = date1;
	}

	public String getDate2() {
		return date2;
	}

	public void setDate2(String date2) {
		this.date2 = date2;
	}

	public int getPlugin() {
		return plugin;
	}

	public void setPlugin(int plugin) {
		this.plugin = plugin;
	}

	public int getChannelId() {
		return channelId;
	}

	public void setChannelId(int channelId) {
		this.channelId = channelId;
	}

	public int getProjectId() {
		return projectId;
	}

	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getDimension() {
		return dimension;
	}

	public void setDimension(String dimension) {
		this.dimension = dimension;
	}

	public List<Channel> getChannels() {
		return channels;
	}

	public void setChannels(List<Channel> channels) {
		this.channels = channels;
	}

	public List<Project> getProjects() {
		return projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	public List<ICpUserData> getDatas() {
		return datas;
	}

	public void setDatas(List<ICpUserData> datas) {
		this.datas = datas;
	}

	public int getPageTag() {
		return pageTag;
	}

	public void setPageTag(int pageTag) {
		this.pageTag = pageTag;
	}
	
	
}
